const db = require('../models');
const { Op } = require('sequelize');
const moment = require('moment');
class couponservice {
    //优惠列表
    async couponList(truckLic, direction) {
            var couponList = [];
            const receiptResult = await db.receipt.findAll({
                where: {
                    Trucklic: truckLic,
                    ActiveTime: {
                        // [Op.lte]: moment().utc('+08:00').format(),
                        [Op.gte]: moment().add(-1, 'day').utc('+08:00').format()
                    }
                },
                attributes: ['ReceiptID', 'ActiveTime'],
                raw: true,
                'order': [
                    ["ActiveTime", "DESC"]
                ]
            })
            for (let index in receiptResult) {
                if (direction == '1') {
                    var couponResult = await db.coupon.findOne({
                        where: {
                            ReceiptID: receiptResult[index].ReceiptID,
                            [Op.or]: [{
                                    StatusNow: {
                                        [Op.in]: [4, 2]
                                    }
                                },
                                {
                                    LastRedeemTime: {
                                        [Op.between]: [moment().add(-10, 'm').utc('+08:00').format(), moment().utc('+08:00').format()],
                                    }
                                }
                            ],
                            // StatusNow: {
                            //     [Op.in]: [4, 2]
                            // }
                        },
                        raw: true
                    })
                } else {
                    var couponResult = await db.coupon.findOne({
                        where: {
                            ReceiptID: receiptResult[index].ReceiptID,
                            // StatusNow: {
                            //     [Op.in]: [4, 3]
                            // }
                            //******* */
                            [Op.or]: [{
                                    StatusNow: {
                                        [Op.in]: [4, 3]
                                    }
                                },
                                {
                                    LastRedeemTime: {
                                        [Op.between]: [moment().add(-10, 'm').utc('+08:00').format(), moment().utc('+08:00').format()],
                                    }
                                }
                            ],
                        },
                        raw: true
                    })
                }

                if (couponResult != null) {
                    var ActiveTime = moment(receiptResult[index].ActiveTime).format('YYYY-MM-DD HH:mm:ss')
                    couponResult.LastRedeemTime = moment(couponResult.LastRedeemTime).utc().format('YYYY-MM-DD HH:mm:ss')
                    couponList.push({...couponResult, 'ActiveTime': ActiveTime })
                }
            }
            if (couponList.length != 0) {
                return { code: '1', data: couponList }
            } else {
                return { code: '0' }
            }
        }
        //使用大桥证
    async useCoupon(params) {
            const { CouponID, Direction, RedeemTime, StatusNow, PhoneNum, Channel, TruckLic } = params;
            try {
                let nowTime = moment.utc(RedeemTime).utcOffset('+08:00').format();
                if (Direction == '0') { //进方向减2
                    var Status = parseInt(StatusNow) - 2;
                    var result = await db.coupon.update({
                        StatusNow: Status,
                        LastRedeemTime: nowTime,
                        RedeemTimeIn: nowTime,
                        RedeemChannelIn: Channel,
                        RedeemLogIn: TruckLic + "+" + PhoneNum,
                    }, {
                        where: {
                            CouponID: CouponID
                        }
                    })
                } else { //出方向减1
                    var Status = parseInt(StatusNow) - 1;
                    var result = await db.coupon.update({
                        StatusNow: Status,
                        LastRedeemTime: nowTime,
                        RedeemTimeOut: nowTime,
                        RedeemChannelOut: Channel,
                        RedeemLogOut: TruckLic + "+" + PhoneNum,
                    }, {
                        where: {
                            CouponID: CouponID
                        }
                    })
                }
                console.log(result)
            } catch (error) {
                console.log(error)
            }

        }
        //当前小票
    async currentReceipt(trucklic) {
        try {
            let obj = new Object();
            let data = new Array();
            const resultR0 = await db.receipt.findAll({
                where: {
                    Trucklic: trucklic,
                    IsReActive: 1,
                    ActiveTime: {
                        [Op.lte]: moment().utc('+08:00').format(),
                        [Op.gte]: moment().add(-1, 'day').utc('+08:00').format()
                    }
                },
                'order': [
                    ["ActiveTime", "DESC"]
                ]
            })
            data.push(resultR0);
            const resultR1 = await db.receipt.findAll({
                where: {
                    Trucklic: trucklic,
                    IsReActive: 0,
                    CreateTime: {
                        [Op.lte]: moment().utc('+08:00').format(),
                        [Op.gte]: moment().add(-1, 'day').utc('+08:00').format()
                    }
                },
                'order': [
                    ["CreateTime", "DESC"]
                ]
            })
            data.push(resultR1);
            if (resultR1.length <= 0 && resultR0.length <= 0) {
                return { "code": 0 }
            } else {
                obj["code"] = 1;
                obj["data"] = data;
                return obj;
            }
        } catch (error) {
            console.log(error);
        }

    }
}
//接口测试
// const service = new couponservice();
// service.couponList('皖BKFT903', 1).then(value => {
//     console.log(value);
// })

module.exports = new couponservice();